#include "new_account.h"
#include "ui_new_account.h"
#include<QMessageBox>
#include<QDebug>
#include<QDesktopWidget>
#include<QtSql/QSqlQuery>
//This class is designed to create new account for uservor allow user to edit account
New_account::New_account(int id,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::New_account)
{
    ui->setupUi(this);
    QRect q=QApplication::desktop()->availableGeometry(this);
    ui->scrollArea->setGeometry(0,0,(q.width()),(q.height()));
    int fontSize=10;
    ui->label->setFont(QFont("lucida",fontSize,QFont::Normal,FALSE));
    ui->label_2->setFont(QFont("lucida",(fontSize-2),QFont::Normal,FALSE));
    ui->label_3->setFont(QFont("lucida",(fontSize-2),QFont::Normal,FALSE));
    ui->save->setFont(QFont("lucida",(fontSize-2),QFont::Normal,FALSE));
    ui->verticalLayout_3->addSpacing(15);
    ui->label->setFocus();
    accid=id;
    //ui->acc_amt->setFocus();
    if(id>0)
    {
        this->setWindowTitle(tr("Edit Account"));
        ui->label->setText(tr("Edit Details"));
        if(db.db_connect())
        {
            QSqlQuery q;
            if(q.exec(QString("select name,account_amt from account_table where account_id=%1").arg(id)))
            {
                q.next();
                ui->acc_name->setText(QString("%1").arg(q.value(0).toString()));
                ui->acc_amt->setText(QString("%1").arg(q.value(1).toDouble()));
            }
            db.db_close();
        }

    }
    ui->acc_amt->setValidator(new QDoubleValidator(this));
    QAction* backAction = new QAction( tr("Back"), this );
     backAction->setSoftKeyRole( QAction::NegativeSoftKey );
     connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
     addAction( backAction );

}

New_account::~New_account()
{
    delete ui;
}

void New_account::on_save_clicked()
{
    QString name=ui->acc_name->text();
    double amount=ui->acc_amt->text().toDouble();

    if(((name.compare(""))==0)||((ui->acc_amt->text()).compare("")==0))
        QMessageBox::information(this,"Error !","Please enter all the details");
    else if(amount<0)
        QMessageBox::information(this,"Error !","Enter new valid amount");
    else
    {
        if(db.db_connect())
        {
             if(accid>0)
            {
                if(db.db_account_update(name,amount,accid))
                 {
                   QMessageBox::information(0,"Success","Account updated successfully!");
                   account *acc=new account;
                   acc->show();
                 }
                else
                   QMessageBox::information(0,"Error","Error in updating account");
            }
             else
             {
              if(db.db_account_insert(name,amount)==0)
                  QMessageBox::information(0,"Error","Please enter another name");
              else
              {
                  QMessageBox::information(0,"Success","Account created successfully!");

       QSqlQuery qr;
       qr.prepare("select * from account_table");
       if(qr.exec())
       {
           while(qr.next())
           {
            int nm=qr.value(0).toInt();
            qDebug()<<nm<<qr.value(1).toString()<<qr.value(2).toDouble()<<qr.value(3).toBool();


       }
        }
           //Please remove comments to see inserted values.


               account *acc=new account;
               acc->show();

            }
           }
           db.db_close();
        }
    }
}
void New_account::back()
{
    (new account)->show();

}

